﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using FVG.ControlPanel.Site.Models;
using LogikBug.ModularMvc;

namespace FVG.ControlPanel.Site.Controllers
{
	[HandleError]
	public class HomeController : ModuleController
	{
		[Authorize]
		public ActionResult Index()
		{
			return View();
		}

		[HttpGet]
		public ActionResult Login()
		{
			return View();
		}

		[HttpPost]
		public ActionResult Login(LoginModel model)
		{
			if (ModelState.IsValid)
			{
				// TODO Add Authentication
				var ticket = new FormsAuthenticationTicket(1, model.UserName, DateTime.Now, DateTime.Now.AddMinutes(20), true, model.UserName);
				string authTicket = FormsAuthentication.Encrypt(ticket);
				Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, authTicket) { Expires = DateTime.Now.AddMinutes(20) });
				return RedirectToAction("Index");
			}

			return View(model);
		}

		public ActionResult Logout()
		{
			FormsAuthentication.SignOut();
			return RedirectToAction("Login");
		}
	}
}
